package com.adong.crowd.mvc.handler;

import com.adong.crowd.entity.Auth;
import com.adong.crowd.entity.Role;
import com.adong.crowd.service.api.AssignService;
import com.adong.crowd.service.api.AuthService;
import com.adong.crowd.service.api.RoleService;
import com.adong.crowd.util.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
public class AssignHandler {


    @Autowired
    private AuthService authService;

    @Autowired
    private AssignService assignService;

    /**
     * 查询分配和未分配的角色集合
     *
     * @param adminId
     * @param modelMap
     * @return
     */
    @RequestMapping("/assign/to/assign/role/page.html")
    public String toAssignRolePage(
            @RequestParam("adminId") Integer adminId,
            ModelMap modelMap) {

        // 查询已分配的角色
        List<Role> assignedRoleList = assignService.getAssignedRole(adminId);
        // 查询未分配角色
        List<Role> unAssignedRoleList = assignService.getUnAssignedRole(adminId);

        // 存入到Request域中
        modelMap.addAttribute("assignedRoleList", assignedRoleList);
        modelMap.addAttribute("unAssignedRoleList", unAssignedRoleList);
        return "assign-role";
    }

    /**
     * 更新用户的角色分配
     */
    @RequestMapping("/assign/do/role/assign.html")
    public String saveAdminRoleRelationship(
            @RequestParam("adminId") Integer adminId,
            @RequestParam("pageNum") Integer pageNum,
            @RequestParam("keyword") String keyword,
            // 可以允许用户取消用户全被分配角色再提交表单,false表示这个请求参数不是必须的
            @RequestParam(value = "roleIdList", required = false) List<Integer> roleIdList) {
        assignService.saveAdminRoleRelationship(adminId, roleIdList);
        return "redirect:/admin/get/page.html?pageNum=" + pageNum + "&keyword=" + keyword;
    }

    /**
     * 查询auth的关系图,返回给zTree生成树形
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("/assgin/get/all/auth.json")
    public ResultEntity<List<Auth>> getAllAuth() {
        List<Auth> authList = authService.getAll();
        System.out.println(authList);
        return ResultEntity.successWithData(authList);
    }

    /**
     * 根据roleId查询inner_role_auth 中authId集合的值
     *
     * @param roleId
     * @return
     */
    @ResponseBody
    @RequestMapping("/assign/get/assigned/auth/id/by/role/id.json")
    public ResultEntity<List<Integer>> getAssignedAuthIdByRoleId(
            @RequestParam("roleId") Integer roleId) {
        List<Integer> authIdList = authService.getAssignedAuthIdByRoleId(roleId);
        return ResultEntity.successWithData(authIdList);
    }

    @ResponseBody
    @RequestMapping("/assign/do/role/assign/auth.json")
    public ResultEntity saveRoleAuthRelationship(
            @RequestBody Map<String, List<Integer>> map
    ) {
        authService.saveRoleAuthRelationship(map);
        return ResultEntity.successWithoutData();
    }
}

